<?php
class CastDictionaryAddAction extends BaseAction 
{
    /**
     * @isnull false
     * @maxLength 255
     * @label Master Name
     */
    public $master;
    public $chi_last_name;
    public $chi_first_name;
    public $eng_last_name;
    public $eng_first_name1;
    public $eng_first_name2;
    public $others;
    public $see_also;
    public $remark;
    public $gender;
    public $is_chinese;
    public $modified_at;

    public $ut;

    function execute()
    {
        #error
		if (count($this->_errors) > 0) {
			$this->init_page_data();
			return 'filter-error';
		}

        #check master name
        if (cast_dictionary::checkExistsByMasterName($this->master)) {
            $this->init_page_data();
            $this->addError('Master name', array('0024' => 'Master name error'));
            return 'filter-error';
        }

        #save
        try {
            $this->add_cast_dictionary();
            return 'succ';
        } catch (Exception $e) {
            return 'system-error';
        }
    }

    private function add_cast_dictionary()
    {
        $cast_dictionary = new cast_dictionary;
        $cast_dictionary->set($this);
        $cast_dictionary->created_at = date('Y-m-d H:i:s');
        $cast_dictionary->created_by = CommonUtils::get_user();
        $cast_dictionary->modified_at = date('Y-m-d H:i:s');
        $cast_dictionary->modified_by = CommonUtils::get_user();

        $insert_id = Model::$dbo['w']->save($cast_dictionary);
        if ($insert_id <= 0) {
			$this->addError("cast_dictionary:".AC::LOG_OP_CREATE.":".$insert_id, "add cast_dictionary failed", json_encode($cast_dictionary));
        }
    }

    private function init_page_data()
    {
        $this->setData();
    }
}
